require(["common", "jQuery", "eventManager", "hash_routie", "coAjax", "href", "queryString", 'js/tools/getarea'], function(jSouper, $, eventManager, hash_routie, coAjax, href, QueryString) {
	jSouper.ready(function() {
		//完成编辑，提交表单
		// var startX;
		// var isStart = false;
		// document.ontouchmove = function(e) {
		// 	if (!isStart) {
		// 		var touch = e.touches[0];
		// 		startX = touch.screenX;
		// 		e.preventDefault();
		// 		isStart = true;
		// 		// console.log(e.target);
		// 	}
		// };
		// document.ontouchend = function(e) {
		// 	if (isStart) {
		// 		isStart = false;
		// 		var touch = e.changedTouches[0];
		// 		var valueX = touch.screenX - startX;
		// 		// console.log(valueX);
		// 		if (valueX < -20) {
		// 			if (App.get('infotype') == 'pwd') {
		// 				App.set('title', '收货地址');
		// 				App.set('infotype', 'addr');
		// 			}
		// 			if (!App.get('infotype')) {
		// 				App.set('title', '密码管理');
		// 				App.set('infotype', 'pwd')
		// 			};
		// 		} else if (valueX > 20) {
		// 			if (App.get('infotype') == 'pwd') {
		// 				App.set('title', '基本信息');
		// 				App.set('infotype', '');
		// 			}
		// 			if (App.get('infotype') == 'addr') {
		// 				App.set('title', '密码管理');
		// 				App.set('infotype', 'pwd');
		// 			};
		// 		} else {
		// 			return;
		// 		}
		// 		e.preventDefault();
		// 	}
		// };
		hash_routie.on("info", function() {
			App.set("thisPage", 4);
			var queryString = new QueryString(location.hash);
			var _type = queryString.get("type");
			if (_type == 2) {
				App.set('title', '密码管理');
				App.set('infotype', 'pwd');
			} else if (_type == 3) {
				App.set('title', '收货地址');
				App.set('infotype', 'addr');
			} else {
				App.set('title', '基本信息');
				App.set('infotype', '');
			}

			// 个人资料
			App.set("$Event.personalinfo.post_edit", function(e) {
				coAjax.put(appConfig.user.set_info_url, App.get("loginer.info"), function(result) {
					// console.log(result);
					alert('success', '修改成功');
					App.set("loginer", result.result)
				}, function(errorCode) {
					switch (errorCode) {
						case "00025":
							alert("error", "登录超时");
							href.toLogin();
							break;
						case "00041":
							alert("error", "请检查您的身份证格式");
							break;
						case "00043":
							alert("error", "请检查您的邮箱格式");
							break;
						case "00044":
							alert("error", "请检查您的手机号码");
							break;
						default:
							alert(arguments[2]);
					}
				});
			});
			// 密码
			App.set("$Event.user.changepwd.change", function(e, vm) {
				var current_pwd = App.get("$Cache.user.changepwd.current_pwd");
				var new_pwd = App.get("$Cache.user.changepwd.new_pwd");
				var new_pwd_bak = App.get("$Cache.user.changepwd.new_pwd_bak");
				if (!current_pwd) {
					alert("warn", "请输入当前密码");
					return;
				}
				if (new_pwd !== new_pwd_bak) {
					alert("warn", "两次密码不匹配")
					return;
				}
				coAjax.put(appConfig.user.change_password, {
					old_password: current_pwd,
					new_password: new_pwd
				}, function(result) {
					App.set("$Cache.user.changepwd.current_pwd", "");
					App.set("$Cache.user.changepwd.new_pwd", "");
					App.set("$Cache.user.changepwd.new_pwd_bak", "");
					alert("success", "密码修改成功");
				}, function(errorCode, xhr, errorMsg) {
					alert("error", errorMsg);
				});
			});
			// 地址
			coAjax.get(appConfig.user.address_detail, function(result) {
				App.set("user.address_detail", result.result);
			});
			// 增加
			App.set("$Event.add_user_address", function() {
				var new_user_address = {
					province: $("[name='province']").find('option:selected').text(),
					town: $("[name='city']").val(),
					county: $("[name='area']").val(),
					// street:
					detail: $("[name='detailed_addre']").val(),
					mobilenumber: $("[name='mobile_num']").val(),
					phonenumber: $("[name='phone_num']").val(),
					postcode: $("[name='zip_code']").val(),
					name: $("[name='name']").val()
				};
				console.log(new_user_address);
				coAjax.post(appConfig.user.add_address, new_user_address, function(result) {
					var address_detail = App.get("user.address_detail");
					address_detail.unshift(result.result);
					App.set("user.address_detail", address_detail);
					App.model.toggle('isnew');
					alert("success", "添加新地址成功！");
					coAjax.put(appConfig.user.set_default_address, {
						address_id: result.result._id
					}, function(result) {
						App.set("loginer.default_ua_id", result.result);
						App.getModel().touchOff("user.address_detail");
						alert('success', '已将新增加地址修改为默认地址！');
					});
				}, function(errorCode, xhr, errorMsg) {
					alert("error", errorMsg);
				});
			}, function(errorCode, xhr, errorMsg) {
				alert("error", errorMsg);
			});
			// 修改默认
			App.set("$Event.user.set_default_address", function(e, vm) {
				coAjax.put(appConfig.user.set_default_address, {
					address_id: vm.get("_id")
				}, function(result) {
					App.set("loginer.default_ua_id", result.result);
					App.getModel().touchOff("user.address_detail");
					alert('success', '更改默认地址成功');
				});
			}, function(errorCode, xhr, errorMsg) {
				alert("error", errorMsg);
			});
			App.set("$Top.$Event.user.remove_address", function(e, vm) {
				coAjax["delete"](appConfig.user.remove_address, {
					address_id: vm.get("_id")
				}, function(result) {
					alert('success', '删除成功');
					App.set("user.address_detail", result.result);
				})
			}, function(errorCode, xhr, errorMsg) {
				alert("error", errorMsg);
			});
		})
	})
})

function getPath(obj) {
	if (obj) {
		var files = obj.files;
		var type = files[0].type;
		if (type === "image/jpeg" || type === "image/bmp") {
			$('img[name="img"]')[0].src = window.URL.createObjectURL(files[0])
			try {
				window.URL.revokeObjectURL(this.src);
			} catch (e) {}
		} else {
			alert('只支持.jpg, .jpeg, .bmp格式图片');
		}
	}
}